package com.blast.manager.ui

import android.content.Intent
import android.view.KeyEvent
import android.view.View
import com.blast.manager.R
import com.blast.manager.ask.QuestionListActivity
import com.blast.manager.blast.BlastActivity
import com.blast.manager.delay.DelayActivity
import com.blast.manager.net.NetCheckActivity
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.event.VoltageSettingsEvent
import com.wxl.common.http.CommonHttpUtil
import com.wxl.common.room.entity.DetonatorEntity
import com.wxl.common.room.helper.DBHelper
import com.wxl.common.serialport.SerialPortManager
import com.wxl.common.util.ObservableUtil
import kotlinx.android.synthetic.main.activity_manager_main_layout.*
import org.greenrobot.eventbus.EventBus

class ManagerMainActivity : AbsActivity() {

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_manager_main_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("爆破管理")

        managerMainDelayPlan.setOnClickListener {
            startActivity(Intent(this,DelayActivity::class.java))
        }

        managerMainCheckNetwork.setOnClickListener {
            startActivity(Intent(this,NetCheckActivity::class.java))
        }

        managerMainBlastControl.setOnClickListener {
            startActivity(Intent(this, BlastActivity::class.java))
        }

        managerMainQuestion.setOnClickListener {
            startActivity(Intent(this, QuestionListActivity::class.java))
        }

        CommonHttpUtil.getDetonatorInfo(object : ObservableUtil.Companion.OnNextCallback<DetonatorEntity>(){
            override fun onNext(t: DetonatorEntity) {
                managerMainBqText.text = "版权所有：${t.deptid}"
            }
        })

        //设置电压
        managerMainVoltageBtn.visibility = if(AppContext.appContext.isShowSetUnder()) View.VISIBLE else View.GONE
        managerMainVoltageBtn.setOnClickListener {
            EventBus.getDefault().post(VoltageSettingsEvent())
        }
    }


    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish()
        }
        if(keyCode == KeyEvent.KEYCODE_F1){
            managerMainDelayPlan.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F2){
            managerMainCheckNetwork.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F3){
            managerMainBlastControl.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_F4){
            managerMainQuestion.performClick()
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        SerialPortManager.close()
        SerialPortManager.closePower()
        DBHelper.clearLeiGuanDelayEntity()
    }
}